package com.zerotwo.exam.center.utils;

import com.zerotwo.exam.center.model.Question;
import com.zerotwo.exam.center.model.Questions;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
@Service
public class InOutExcel {
//    public void exportExcel(List<Question> list, HttpServletResponse response) throws IOException {
//        //创建wb
//        HSSFWorkbook wb = new HSSFWorkbook();
//        //创建页
//        HSSFSheet sheet = wb.createSheet("题库数据");
//        //创建行
//        HSSFRow row = sheet.createRow(0);
//        //创建第一行单元格
//        HSSFCell cell = row.createCell(0);
//        cell.setCellValue("id");
//
//        cell = row.createCell(1);
//        cell.setCellValue("content");//题目名称
//
//        cell = row.createCell(2);
//        cell.setCellValue("type");//题目类型
//
//        cell = row.createCell(3);
//        cell.setCellValue("analysis");//题目解析
//
//        cell = row.createCell(4);
//        cell.setCellValue("difficulty");//题目难度
//
//        cell = row.createCell(5);
//        cell.setCellValue("score");//题目分值
//
//        cell = row.createCell(6);
//        cell.setCellValue("is_public");//题目是否公开
//
//        cell = row.createCell(7);
//        cell.setCellValue("course_id");//课程id
//
//        cell = row.createCell(8);
//        cell.setCellValue("tag_id");//题目标签id
//
//        cell = row.createCell(9);
//        cell.setCellValue("teacher_id");//教师id
//
//        if(list != null && list.size() > 0){
//            Question question = null;
//            for(int i = 0; i < list.size(); i ++){
//
//                question = list.get(i);
//                //每创建一个对象，就创建一行
//                row = sheet.createRow(i + 1);
//                cell = row.createCell(0);
//                cell.setCellValue(question.getId());
//
//                cell = row.createCell(1);
//                cell.setCellValue(question.getContent());
//
//                cell = row.createCell(2);
//                cell.setCellValue(question.getType().getValue());
//
//                cell = row.createCell(3);
//                cell.setCellValue(question.getAnalysis());
//
//                cell = row.createCell(4);
//                cell.setCellValue(question.getDifficulty());
//
//                cell = row.createCell(5);
//                cell.setCellValue(question.getScore());
//
//                cell = row.createCell(6);
//                cell.setCellValue(question.getIsPublic().getValue());
//
//                cell = row.createCell(7);
//                cell.setCellValue(question.getCourseId());
//
//                cell = row.createCell(8);
//                Integer tagId = question.getTagId();
//                if (tagId != null) {
//                    cell.setCellValue(tagId.intValue());
//                } else {
//                    // 可以选择设置默认值或者空字符串
//                    cell.setCellValue(""); // 或者 cell.setCellValue(0);
//                }
//
//                cell = row.createCell(9);
//                cell.setCellValue(question.getTeacherId());
//            }
//        }
//        // 设置响应内容类型为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet，指定字符集为UTF-8
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
//        response.setHeader("Content-Disposition", "attachment; filename=qu.xlsx");
//
//        // 获取响应输出流，并写入Excel工作簿内容
//        OutputStream out = response.getOutputStream();
//        wb.write(out);
//        wb.close();
//        out.flush();
//    }
    public void exportExcel(List<Question> list, HttpServletResponse response) throws IOException {
        // 创建wb
        HSSFWorkbook wb = new HSSFWorkbook();
        // 创建页
        HSSFSheet sheet = wb.createSheet("题库数据");

        // 创建表头行
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell;

        // 设置表头
        String[] headers = {"id", "content", "type", "analysis", "difficulty", "score", "is_public", "course_id", "tag_id", "teacher_id","content","questionId","answer"};
        for (int i = 0; i < headers.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据
        if (list != null && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                Question questions = list.get(i);
                row = sheet.createRow(i + 1); // 从第二行开始填充数据

                cell = row.createCell(0);
                cell.setCellValue(questions.getId());

                cell = row.createCell(1);
                cell.setCellValue(questions.getContent());

                cell = row.createCell(2);
                Integer  type = questions.getType().getValue();
                if (type != null) {
                    cell.setCellValue(type.intValue());
                } else {
                    cell.setCellValue(0); // 或者 cell.setCellValue(0);
                }

                cell = row.createCell(3);
                cell.setCellValue(questions.getAnalysis());

                cell = row.createCell(4);
                cell.setCellValue(questions.getDifficulty());

                cell = row.createCell(5);
                cell.setCellValue(questions.getScore());

                // 这里是返回数字0，则是默认值
                cell = row.createCell(6);
                Integer  IsPublic = questions.getIsPublic().getValue();
                if (IsPublic != null) {
                    cell.setCellValue(IsPublic.intValue());
                } else {
                    cell.setCellValue(0); // 或者 cell.setCellValue(0);
                }

                cell = row.createCell(7);
                cell.setCellValue(questions.getCourseId());

                cell = row.createCell(8);
                Integer tagId = questions.getTagId();
                if (tagId != null) {
                    cell.setCellValue(tagId.intValue());
                } else {
                    cell.setCellValue(""); // 或者 cell.setCellValue(0);
                }

                cell = row.createCell(9);
                cell.setCellValue(questions.getTeacherId());

                cell = row.createCell(10);
                cell.setCellValue(questions.getContents());

                cell = row.createCell(11);
                cell.setCellValue(questions.getQuestionId());

                cell = row.createCell(12);
                cell.setCellValue(questions.getAnswer());
            }
        }

        // 写入到输出流
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        wb.write(out);
        byte[] bytes = out.toByteArray();
        // 设置HTTP响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        String fileName = "题库.xlsx";
        response.setHeader("Content-disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode(fileName, StandardCharsets.UTF_8)); // 使用 UTF-8 编码文件名，并加上 filename* 参数
        response.setContentLength(bytes.length); // 设置 Content-Length

        // 通过输出流将Excel文件下载到客户端浏览器
        try (OutputStream outputStream = response.getOutputStream()) {
            outputStream.write(bytes);
            outputStream.flush();
        }
        wb.close(); // 关闭 Workbook
    }
}
